package leetcode.hot100;

import java.util.Arrays;

public class Solution312 {
    //给一数组nums，每次删去1个数获得奖励nums[i-1]*nums[i]*nums[i+1]，求最大奖励
    //如果下标超出范围就乘1

    public static void main(String[] args) {

        int[] nums = {3,1,5,8,2,5,7,9,0,3};
        new Solution312().maxCoins(nums);
    }

    public int maxCoins(int[] nums) {
        //dp[i][j]定义为区间（i，j）内气球戳破获得的最大收益
        //那么dp[i][j]=max(dp[i][k]+dp[k][j]+nums[i]*nums[k]*nums[j]),i<k<j
        //理解：假设k是区间（i，j）戳破的最后一个气球，那么戳破k后的收益就是上面max里的表达式

        //两边补1
        int len = nums.length;
        int[] newNums = new int[len+2];
        Arrays.fill(newNums,1);
        System.arraycopy(nums,0,newNums,1,len);
        nums = newNums;

        //开始dp
        len += 2;
        int[][] dp = new int[len][len];
        //沿着对角线
        for (int col = 2; col < len; col++) {
            for (int i = 0,j = col; j < len; i++,j++) {
                dp[i][j] = -1;
                for (int k = i+1; k < j; k++) {
                    dp[i][j] = Math.max(dp[i][j],dp[i][k]+dp[k][j]+nums[i]*nums[k]*nums[j]);
                }
            }
        }
//        //从下向上一行一行
//        for (int i = len-3; i >= 0; i--) {
//            for (int j = i+2; j < len; j++) {
//                for (int k = i+1; k < j; k++) {
//                    count++;
//                    dp[i][j] = Math.max(dp[i][j],dp[i][k]+dp[k][j]+nums[i]*nums[k]*nums[j]);
//                }
//            }
//        }
        return dp[0][len-1];
    }
}
